package com.xuetang.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 血糖记录实体
 */
public class BloodSugarRecord implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 记录ID
     */
    private Long id;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 血糖值mmol/L
     */
    private BigDecimal value;

    /**
     * 测量时间
     */
    private LocalDateTime measureTime;

    /**
     * 测量类型：1空腹，2餐前，3餐后1小时，4餐后2小时，5睡前，6凌晨，7随机
     */
    private Integer measureType;

    /**
     * 备注
     */
    private String remark;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    // 额外属性，非数据库字段
    private String userName;
    private String measureTypeName;
    private String statusName;

    // Getter and Setter methods
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public BigDecimal getValue() {
        return value;
    }

    public void setValue(BigDecimal value) {
        this.value = value;
    }

    public LocalDateTime getMeasureTime() {
        return measureTime;
    }

    public void setMeasureTime(LocalDateTime measureTime) {
        this.measureTime = measureTime;
    }

    public Integer getMeasureType() {
        return measureType;
    }

    public void setMeasureType(Integer measureType) {
        this.measureType = measureType;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getMeasureTypeName() {
        return measureTypeName;
    }

    public void setMeasureTypeName(String measureTypeName) {
        this.measureTypeName = measureTypeName;
    }

    public String getStatusName() {
        return statusName;
    }

    public void setStatusName(String statusName) {
        this.statusName = statusName;
    }

    /**
     * 获取测量类型的名称
     */
    public String getMeasureTypeText() {
        if (measureType == null) {
            return "";
        }
        switch (measureType) {
            case 1:
                return "空腹";
            case 2:
                return "餐前";
            case 3:
                return "餐后1小时";
            case 4:
                return "餐后2小时";
            case 5:
                return "睡前";
            case 6:
                return "凌晨";
            case 7:
                return "随机";
            default:
                return "未知";
        }
    }

    /**
     * 获取状态的名称
     */
    public String getStatusText() {
        // 没有值或测量类型，无法判断状态
        if (value == null || measureType == null) {
            return "";
        }

        double val = value.doubleValue();
        int type = measureType;

        // 根据测量类型确定正常范围
        double lowerLimit, upperLimit;
        switch (type) {
            case 1: // 空腹
                lowerLimit = 3.9;
                upperLimit = 6.1;
                break;
            case 2: // 餐前
                lowerLimit = 3.9;
                upperLimit = 6.1;
                break;
            case 3: // 餐后1小时
                lowerLimit = 3.9;
                upperLimit = 10.0;
                break;
            case 4: // 餐后2小时
                lowerLimit = 3.9;
                upperLimit = 7.8;
                break;
            case 5: // 睡前
                lowerLimit = 3.9;
                upperLimit = 7.0;
                break;
            default:
                lowerLimit = 3.9;
                upperLimit = 7.8;
                break;
        }

        // 判断状态
        if (val < lowerLimit) {
            return "偏低";
        } else if (val > upperLimit) {
            return "偏高";
        } else {
            return "正常";
        }
    }
}